/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.memberView;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TShopCharge;
import com.ruipengkj.bean.TShopRecharge;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.service.TBillDetailService;
import com.ruipengkj.service.TShopChargeService;
import com.ruipengkj.service.TShopRechargeService;
import com.ruipengkj.service.TUserService;
import com.ruipengkj.view.base.BaseDialog;

/**
 * 说明：
 * 会员详细Dialog
 * @Author：李智深
 * @Date：2012-03-20
 */
public class MemberChargeDetailDialog extends BaseDialog{

	private static final long serialVersionUID = 1L;
	private JLabelJTextFieldComponent fillMoneyTF = null; //充值金额
	private JLabelJTextFieldComponent giftMoneyTF = null; //充值金额
	private TUserService tUserService;//用户操作 
	private TShopCharge tShopCharge;
	private TShopChargeService tShopChargeService;
	private String userID;//会员编号
	private TBillDetailService tBillDetailService;
	private TShopRechargeService tShopRechargeService;
	private JButton sureButton = null; //确定按钮
	private JButton cancelButton = null; //取消
	List<TUser> tUser = new ArrayList();
	public MemberChargeDetailDialog(JDialog jd, String title, boolean b) {	
		super(jd, title, b);
		tShopChargeService=BuildSpringApplication.getInstance().getTShopChargeServiceImpl();
		tUserService=BuildSpringApplication.getInstance().getTUserServiceImpl();
		tBillDetailService = BuildSpringApplication.getInstance().getTBillDetailServiceImpl();
		tShopRechargeService = BuildSpringApplication.getInstance().getTShopRechargeServiceImpl();
		this.init();
		this.setSize(300, 550);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}
	private void init(){
		this.initViews();
		this.initListener();
	}
	
	private void initViews(){
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setBackground(Util.PANEL_COLOR);

		fillMoneyTF=new JLabelJTextFieldComponent("充值金额:",30);
		giftMoneyTF=new JLabelJTextFieldComponent("赠送金额:",30);
		giftMoneyTF.getText().setEditable(false);
		JPanel jp1=new JPanel();
		sureButton=new RPJButtonBlue("确定");
		cancelButton = new RPJButtonGray("取消");
		jp1.add(sureButton);
		jp1.add(cancelButton);
		jp1.setOpaque(false);
		
		main_jp.add(fillMoneyTF);
		main_jp.add(giftMoneyTF);
		main_jp.add(jp1);
		
		this.add(main_jp);
	}
	private void initListener(){
		sureButton.addActionListener(this);
		sureButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		cancelButton.addActionListener(this);
		fillMoneyTF.getText().addKeyListener(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==sureButton){
			Boolean isShow =false;
			//确定
			String fillMoneyT=fillMoneyTF.getText().getText().trim(); //充值金额
			String giftMoneyT=giftMoneyTF.getText().getText().trim();//赠送金额
			if(!MatcherFormatUtil.MatcherPriceScheme(fillMoneyT)){
				JOptionPane.showMessageDialog(null, "充值金额 不能为空，且小数点左边最多10位数字，右边最多2位数字");
				fillMoneyTF.getText().setText("");
				fillMoneyTF.getText().requestFocus();
				return;
			}
			if(!MatcherFormatUtil.MatcherPriceScheme(giftMoneyT)){
				JOptionPane.showMessageDialog(null, "请在充值金额栏点击回车获取赠送金额！");
				giftMoneyTF.getText().setText("");
				giftMoneyTF.getText().requestFocus();
				return;
			}
			for(int i =0 ; i < tUser.size(); i++){
				if(giftMoneyTF.getText().getText().trim()!=""){
					Double totalMoney = Double.parseDouble(giftMoneyTF.getText().getText().trim()) + Double.parseDouble(fillMoneyT);
					tUser.get(i).setMoney(totalMoney + tUser.get(i).getMoney());
				}else{
					tUser.get(i).setMoney(Double.parseDouble(fillMoneyT));
				}
				try {
					Date date = new Date();
					if(tUserService.modifyRechargeService(tUser.get(i))){
						BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("会员充值，卡号为：+"+tUser.get(i).getCardNum()+"，充值金额："+fillMoneyT);
						
						TShopRecharge tShopRecharge = new TShopRecharge();//添加明细
						tShopRecharge.setRechargeID(UUID.GenUUID15());
						tShopRecharge.setUserID(userID);// 单据编号CHAR(15)
						tShopRecharge.setChargeRuleID("");
						tShopRecharge.setHappenTime(date);
						tShopRecharge.setMoney(Double.parseDouble(fillMoneyT));
						tShopRecharge.setGiftMoney(Double.parseDouble(giftMoneyTF.getText().getText().trim()));
						tShopRechargeService.saveService(tShopRecharge);
						//添加资金出入
						ApplicationContext.addMondyInOut(ApplicationContext.MONEYINOUT_TYPE_IN, 
								ApplicationContext.OPERATOR_ID, ApplicationContext.MONEYINOUT_SOURCE_TYPE5, tShopRecharge.getRechargeID());
						isShow=true;
					}
				} catch (Exception e1) {
					isShow=false;
					e1.printStackTrace();
				}
				
			}
			if(isShow){
				JOptionPane.showMessageDialog(null, "充值成功!");
				this.dispose();
			}else{
				JOptionPane.showMessageDialog(null, "数据异常。");
			}
		}else if(e.getSource()==cancelButton){
			this.dispose();
		}
	}
	@Override
	public void keyReleased(KeyEvent e) {
		if(e.getSource()==fillMoneyTF.getText()){
			if(e.getKeyCode()==KeyEvent.VK_ENTER){
				TShopCharge tShopCharge=new TShopCharge();
				tShopCharge.setAMount(Double.parseDouble(fillMoneyTF.getText().getText()));
			    try {
					PageModel<TShopCharge> pageModel=tShopChargeService.getTShopChargeService(0, 2,tShopCharge);
					if(pageModel!=null){
						giftMoneyTF.getText().setText(pageModel.getResultList().get(0).getGiftAmount()+"");
					}else{
						giftMoneyTF.getText().setText(0+"");
					}
				} catch (Exception e1) { 
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
	}
public void showDialog(List<TUser> user){
		for(int i =0;i<user.size();i++){
			tUser.add(user.get(i));
		}
		this.setVisible(true);
	}
}
